/*
 * @Author: HaiHe_Team
 * @Date: 2023-02-07 14:12:58
 * @LastEditTime: 2023-02-07 15:08:46
 * @LastEditors: Joel
 * @Description: HaiHe Copyright 2022
 * @FilePath: \vscode_test_c_code\src\hlw8012.c
 */
#include "hlw8012.h"
/**
 * @description: @description计算功率值
 * @param {uint16_t} frequency测量的功率对应的频率;
 * @param {uint16_t} phase_V测量出的电压
 * @return {*}计算结果功率
 */
short algorithm_hlw8012_power_value(short frequency, short phase_V)
{
    // V2P公式通过频率真反推导出 V2P参数电压
    static short algorithm_power = 0;
    static float index_v1_v2p = 0.0;
    static float index_v2p = 0.0;
    static float index_v1p = 0.0;
    index_v1_v2p = ((float)frequency * VERF_POWER) / FOSC_POWER / 48;
    printf("index_v1_v2p= %f\r\n", index_v1_v2p);
    index_v2p = ((float)phase_V / RESISTOR_VALUE) / RESISTOR_DOWN;
    printf("index_v2p= %f\r\n", index_v2p);
    index_v1p = (index_v1_v2p / index_v2p);
    printf("index_v1p= %f\r\n", index_v1p);
    index_v1p = index_v1p / RESISTOR_CURRENT;
    printf("index_v1p= %f\r\n", index_v1p);
    index_v1p = index_v1p * CT_RATE;
    printf("index_v1p= %f\r\n", index_v1p);
    index_v1p = index_v1p * (float)phase_V;
    printf("index_v1p= %f\r\n", index_v1p);
    algorithm_power = (short)(index_v1p / POWER_RATE);
    // 转为16位返回
    return algorithm_power;
}